import React from "react";

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      number: Math.floor(Math.random() * 3),
    };
  }

  handleChange = () => {
    this.setState(
      (state, props) => {
        // console.log(state)
        return {
          number: Math.floor(Math.random() * 3),
        };
      },
      () => {
        console.log(this.state);
      }
    );
  };

  render() {
    console.log("render：", this.state.number);
    return (
      <div>
        <h1>{this.state.number}</h1>
        <button onClick={this.handleChange}>生成</button>
      </div>
    );
  }

  // 因为随机数可能相同，相同时不需要重新渲染，在shouldComponentUpdate方法中做判断
}

export default App;
